<?php
namespace Library;


use Philo\Blade\Blade;


/**
* View
*/
class View {
	//本类单实例变量
	static private $inst = NULL;
	//blade模板实例
	private $blade = NULL;


	/**
	 * 构造函数
	 * 依赖外部传入参数，用于实例化blade模板类
	 */
	private function __construct() {
		//default module
		$viewPath = APP_PATH . 'view';
		$cachePath = PRJ_PATH . 'runtime/cache';
		if (!is_dir($cachePath)) {
			mkdir($cachePath, 0755, true);
		}
		$this->blade = new Blade($viewPath, $cachePath);
	}


	/**
	 * 魔术方法，统一处理静态函数的调用
	 * @param string $tpl
	 * @param array $data
	 */
	public static function display($tpl, $data = []) {
		self::getInst()->process($tpl, $data);
	}


	/**
	 * 获取本类实例
	 */
	static private function getInst() {
		if (null === self::$inst) {
			self::$inst = new self();
		}
		return self::$inst;
	}


	/**
	 * 调用blade实例的方法，输出html
	 * @param string $tpl
	 * @param array $data
	 */
	private function process($tpl, $data) {
		exit($this->blade->view()->make($tpl, $data)->render());
	}


}